package com.xmb.wechat.util;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.widget.TextView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author lijiangop
 * @CreateTime 2020/5/18 16:12
 */
public class SpanStringUtils {

    private static final String regexHttpUrl = "(https?://(w{3}\\.)?)?\\w+\\.\\w+(\\.[a-zA-Z]+)*(:\\d{1,5})?(/\\w*)*(\\??(.+=.*)?(&.+=.*)?)?";
    private static final String regexMail    = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?";//邮箱
    private static final String regexIP      = "[1-9](\\d{1,2})?\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))";

    //    private static final String regexPhone  = "(13|14|15|18|17|19|16)[0-9]{9}";//手机号码
    //    private static final String regexTel    = "(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$";//固定电话
    //    private static final String regexIDC    = "[1-9]\\d{13,16}[a-zA-Z0-9]{1}";//身份证
    private static final String regexSPhone = "400-[016789]\\d{2}-\\d{4}";//400 客服号

    private static final String regexNumber = "[0-9]{7,24}";//数字


    /**
     * 获取带表情的文字内容
     *
     * @param context
     * @param tv
     * @param source
     * @return
     */
    public static SpannableString getEmotionContent(final Context context, final TextView tv, String source) {
        SpannableString spannableString = new SpannableString(source);
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT)
            return spannableString;
        if (TextUtils.isEmpty(source))
            return spannableString;
        Resources res = context.getResources();
        //表情
        String regexEmotion = "\\[([\u4e00-\u9fa5\\w])+\\]";
        Pattern patternEmotion = Pattern.compile(regexEmotion);
        Matcher matcherEmotion = patternEmotion.matcher(spannableString);
        int size = (int) tv.getTextSize() * 14 / 10;
        while (matcherEmotion.find()) {
            String key = matcherEmotion.group();
            int start = matcherEmotion.start();
            int imgRes = EmotionUtils.getImgByName(key);
            if (imgRes > -1) {
                Bitmap bitmap = BitmapFactory.decodeResource(res, imgRes);
                Bitmap scaleBitmap = Bitmap.createScaledBitmap(bitmap, size, size, true);
                ImageSpan span = new ImageSpan(context, scaleBitmap);
                spannableString.setSpan(span, start, start + key.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#248bd6"));
        //数字大于等于7
        Pattern patternNumber = Pattern.compile(regexNumber);
        Matcher matcherNumber = patternNumber.matcher(spannableString);
        while (matcherNumber.find()) {
            String key = matcherNumber.group();
            int start = matcherNumber.start();
            spannableString.setSpan(colorSpan, start, start + key.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        //网址 变蓝
        Pattern patternHttpUrl = Pattern.compile(regexHttpUrl);
        Matcher matcherHttpUrl = patternHttpUrl.matcher(spannableString);
        while (matcherHttpUrl.find()) {
            String key = matcherHttpUrl.group();
            int start = matcherHttpUrl.start();
            spannableString.setSpan(colorSpan, start, start + key.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        //IP 变蓝
        Pattern patternIP = Pattern.compile(regexIP);
        Matcher matcherIP = patternIP.matcher(spannableString);
        while (matcherIP.find()) {
            String key = matcherIP.group();
            int start = matcherIP.start();
            spannableString.setSpan(colorSpan, start, start + key.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        //邮箱 变蓝
        Pattern patternMail = Pattern.compile(regexMail);
        Matcher matcherMail = patternMail.matcher(spannableString);
        while (matcherMail.find()) {
            String key = matcherMail.group();
            int start = matcherMail.start();
            spannableString.setSpan(colorSpan, start, start + key.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        //3大运营商号码
        if (source.contains("10086")) {
            int start = source.indexOf("10086");
            spannableString.setSpan(colorSpan, start, start + 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        if (source.contains("10000")) {
            int start = source.indexOf("10000");
            spannableString.setSpan(colorSpan, start, start + 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        if (source.contains("10010")) {
            int start = source.indexOf("10010");
            spannableString.setSpan(colorSpan, start, start + 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        //400客服号 变蓝
        Pattern patternSPhone = Pattern.compile(regexSPhone);
        Matcher matcherSPhone = patternSPhone.matcher(spannableString);
        while (matcherSPhone.find()) {
            String key = matcherSPhone.group();
            int start = matcherSPhone.start();
            spannableString.setSpan(colorSpan, start, start + key.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableString;
    }

    /**
     * 文字获取表情
     * @param context
     * @param tv
     * @param source
     * @return
     */
    public static SpannableString getOnlyEmotion(final Context context, final TextView tv, String source) {
        SpannableString spannableString = new SpannableString(source);
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT)
            return spannableString;
        if (TextUtils.isEmpty(source))
            return spannableString;
        Resources res = context.getResources();
        //表情
        String regexEmotion = "\\[([\u4e00-\u9fa5\\w])+\\]";
        Pattern patternEmotion = Pattern.compile(regexEmotion);
        Matcher matcherEmotion = patternEmotion.matcher(spannableString);
        int size = (int) tv.getTextSize() * 14 / 10;
        while (matcherEmotion.find()) {
            String key = matcherEmotion.group();
            int start = matcherEmotion.start();
            int imgRes = EmotionUtils.getImgByName(key);
            if (imgRes > -1) {
                Bitmap bitmap = BitmapFactory.decodeResource(res, imgRes);
                Bitmap scaleBitmap = Bitmap.createScaledBitmap(bitmap, size, size, true);
                ImageSpan span = new ImageSpan(context, scaleBitmap);
                spannableString.setSpan(span, start, start + key.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        return spannableString;
    }
}
